﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace InpatientManagementSystem
{
    public partial class Form1 : Form
    {
        MySqlConnection myConnection = new MySqlConnection("server=localhost;user=root;password=786452as;database=zhuyuan;port=3306;charset=utf8");
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            //Form4 form4 = new Form4();
            //form4.ShowDialog();
            //if (!form4.GetFlag())
            //{
            //    Close();
            //}
            //if (form4.getFlag())
            //{
                科室信息管理ToolStripMenuItem.Visible = true;
            //}
        }
        /// <summary>
        /// 入院管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem1_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            panel2.Visible = false;
            tabControl2.Visible = false;
            tabControl3.Visible = false;
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            if(item.Name== "入院信息查询")
            {
                tabControl1.SelectedTab = tabControl1.TabPages[1];
            }
            else
            {
                tabControl1.SelectedTab = tabControl1.TabPages[0];
            }
            tabControl1.Visible = true;
        }

        /// <summary>
        /// 清空输入框
        /// </summary>
        private void clearTextBox()
        {
            foreach (Control c in this.tabControl1.TabPages[0].Controls)
            {
                if (c is TextBox)
                {
                    ((TextBox)c).Text = "";
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            clearTextBox();
        }

        /// <summary>
        /// 添加到ListView中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            ListViewItem lvi = new ListViewItem(textBox1.Text);
            bool flag = false;
            foreach (Control c in tabControl1.TabPages[1].Controls)
            {
                if (c is TextBox)
                {
                    if (c.Text == "")
                    {
                        flag = true;
                        break;
                    }
                }
                
            }
            if (flag)
            {
                MessageBox.Show("不能为空!");
            }
            else
            {
                lvi.SubItems.Add(textBox2.Text);
                lvi.SubItems.Add(textBox3.Text);
                lvi.SubItems.Add(textBox4.Text);
                lvi.SubItems.Add(textBox5.Text);
                lvi.SubItems.Add(textBox6.Text);
                this.listView1.Items.Add(lvi);
                clearTextBox();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
        }
        
        /// <summary>
        /// 录入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            myConnection.Open();
            int nRowOrder = 0;
            int nRowOrders = listView1.Items.Count;//行数
            string query="";
            for (nRowOrder = 0; nRowOrder < nRowOrders; nRowOrder++)//遍历listView1的每一行
            {
                ListViewItem lvi = listView1.Items[nRowOrder];
                query = "INSERT INTO patient VALUES ('" + lvi.SubItems[0].Text + "','";
                query = query + lvi.SubItems[1].Text + "','";
                query = query + lvi.SubItems[2].Text + "','";
                query = query + lvi.SubItems[4].Text + "','";
                query = query + lvi.SubItems[5].Text + "','";
                query = query + lvi.SubItems[3].Text + "')";
            }
            MySqlCommand myCommand = new MySqlCommand(query, myConnection);
            int n = myCommand.ExecuteNonQuery();
            if (n > 0)
            {
                MessageBox.Show("成功！");
                listView1.Items.Clear();
                clearTextBox();
            }
            else
            {
                MessageBox.Show("失败！请检查输入");
            }
            myConnection.Close();
        }

        /// <summary>
        /// 入院信息查询,修改
        /// </summary>
        DataSet ds = null;
        MySqlDataAdapter myadp = null;
        private void button6_Click(object sender, EventArgs e)
        {
            MySqlCommandBuilder scb = new MySqlCommandBuilder(myadp);

            //更新数据
            try
            {
                //这里是关键
                myadp.Update(ds);
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string query = "select pat_name as '姓名',sex as '性别',diagnosis as '诊断',doc_name as '主治医生'," +
                "ward_number as '病房号' from patient,doctor where work_number = charge_doc && medi_rec_number like '%";
            query += textBox7.Text;
            query += "%' || pat_name like '%";
            query += textBox7.Text;
            query += "%' || sex like '%";
            query += textBox7.Text;
            query += "%' || diagnosis like '%";
            query += textBox7.Text;
            query += "%' || charge_doc like '%";
            query += textBox7.Text;
            query += "%' || ward_number like '%";
            query += textBox7.Text+ "%'";
            myConnection.Open();
            ds = new DataSet();
            BindingSource bs = new BindingSource();
            myadp = new MySqlDataAdapter(query, myConnection); //适配器   
            myadp.Fill(ds, "table"); //将查询到数据填充到数据集
            bs.DataSource = ds.Tables["table"];
            dataGridView1.DataSource = bs;
            myConnection.Close();
        }


        //操作病人模块
        private void MenuItem2_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            panel2.Visible = false;
            tabControl1.Visible = false;
            tabControl3.Visible = false;
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            if (item.Name == "检查项目")
            {
                tabControl2.SelectedTab = tabControl2.TabPages[0];
            }
            else
            {
                tabControl2.SelectedTab = tabControl2.TabPages[1];
            }
            tabControl2.Visible = true;
        }

        //检查项目
        string input = "";
        DataSet ds2 = null;
        MySqlDataAdapter myadp2 = null;
        private void textBox8_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(System.Windows.Forms.Keys.Enter))
            {
                if(input == textBox8.Text)
                {
                    MessageBox.Show("已显示病人信息！");
                }
                else if(textBox8.Text == "")
                {
                    MessageBox.Show("不能为空！");
                }
                else
                {
                    myConnection.Open();
                    label10.Text = "姓名：";
                    label11.Text = "诊断：";
                    input = textBox8.Text;
                    string query1 = "select pat_name, diagnosis from patient where medi_rec_number = '" + textBox8.Text + "'";
                    string query2 = "select insp_date as '添加日期',insp_pro as '检查项目',isnp_duration as '检查详情' from medical_record_insp where insp_pro != '无' && medi_rec_number = '" + textBox8.Text + "'";
                    MySqlCommand myCommand = new MySqlCommand(query1, myConnection);
                    MySqlDataReader myReader = myCommand.ExecuteReader();
                    Boolean flag = true;
                    try
                    {
                        if (!myReader.Read())
                        {
                            MessageBox.Show("没有该病历号！");
                            flag = false;
                        }
                        else
                        {
                            label10.Text += myReader.GetString(0);
                            label11.Text += myReader.GetString(1);
                            button7.Enabled = true;
                            textBox8.Text = "";
                        }
                    }
                    finally
                    {
                        myReader.Close();
                        myConnection.Close();
                    }
                    if (flag)
                    {
                        try
                        {
                            ds2 = new DataSet();
                            BindingSource bs = new BindingSource();
                            myadp2 = new MySqlDataAdapter(query2, myConnection); //适配器   
                            myadp2.Fill(ds2, "table"); //将查询到数据填充到数据集
                            bs.DataSource = ds2.Tables["table"];
                            dataGridView2.DataSource = bs;
                        }
                        finally
                        {
                            myConnection.Close();
                        }
                    }
                }
            }
        }

        //添加数据后更新列表
        private void button7_Click(object sender, EventArgs e)
        {
            if (label10.Text == "姓名：")
            {
                MessageBox.Show("请先输入病历号！");
            }
            else
            {
                Form2 form2 = new Form2();
                form2.ShowDialog();
                myConnection.Open();
                string query = "INSERT into medical_record_insp(medi_rec_number,pat_name,insp_pro, isnp_duration) values(" + input;
                query = query + ",'" + label10.Text.Substring(3)+ "','"+ form2.getInsp() + "','" + form2.getDetail() + "')";
                MySqlCommand myCommand = new MySqlCommand(query, myConnection);
                int n = myCommand.ExecuteNonQuery();
                if (n > 0)
                {
                    try
                    {
                        ds2 = new DataSet();
                        BindingSource bs = new BindingSource();
                        string query2 = "select insp_date as '添加日期',insp_pro as '检查项目',isnp_duration as '检查详情' from medical_record_insp where insp_pro != '无' && medi_rec_number = '" + input + "'";
                        myadp2 = new MySqlDataAdapter(query2, myConnection); //适配器   
                        myadp2.Fill(ds2, "table"); //将查询到数据填充到数据集
                        bs.DataSource = ds2.Tables["table"];
                        dataGridView2.DataSource = bs;
                    }
                    finally
                    {
                        myConnection.Close();
                    }
                }
                else
                {
                    MessageBox.Show("失败！请检查输入");
                }
                myConnection.Close();
            }
        }

        //开方
        string input2 = "";
        DataSet ds3 = null;
        MySqlDataAdapter myadp3 = null;
        private void textBox9_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(System.Windows.Forms.Keys.Enter))
            {
                if (input2 == textBox9.Text)
                {
                    MessageBox.Show("已显示病人信息！");
                }
                else if (textBox9.Text == "")
                {
                    MessageBox.Show("不能为空！");
                }
                else
                {
                    myConnection.Open();
                    label14.Text = "姓名：";
                    label12.Text = "诊断：";
                    input2 = textBox9.Text;
                    string query1 = "select pat_name, diagnosis from patient where medi_rec_number = '" + textBox9.Text + "'";
                    string query2 = "select insp_date as '添加日期',prescription as '处方',prescrip_duration as '处方详情' from medical_record_pres where prescription != '无' && medi_rec_number = '" + textBox9.Text + "'";
                    MySqlCommand myCommand = new MySqlCommand(query1, myConnection);
                    MySqlDataReader myReader = myCommand.ExecuteReader();
                    Boolean flag = true;
                    try
                    {
                        if (!myReader.Read())
                        {
                            MessageBox.Show("没有该病历号！");
                            flag = false;
                        }
                        else
                        {
                            label14.Text += myReader.GetString(0);
                            label12.Text += myReader.GetString(1);
                            button8.Enabled = true;
                            textBox9.Text = "";
                        }
                    }
                    finally
                    {
                        myReader.Close();
                        myConnection.Close();
                    }
                    if (flag)
                    {
                        try
                        {
                            ds3 = new DataSet();
                            BindingSource bs = new BindingSource();
                            myadp3 = new MySqlDataAdapter(query2, myConnection); //适配器   
                            myadp3.Fill(ds3, "table"); //将查询到数据填充到数据集
                            bs.DataSource = ds3.Tables["table"];
                            dataGridView3.DataSource = bs;
                        }
                        finally
                        {
                            myConnection.Close();
                        }
                    }
                }
            }
        }

        //添加数据后更新列表
        private void button8_Click(object sender, EventArgs e)
        {
            if (label14.Text == "姓名：")
            {
                MessageBox.Show("请先输入病历号！");
            }
            else
            {
                Form3 form3 = new Form3();
                form3.ShowDialog();
                myConnection.Open();
                string query3 = "INSERT into medical_record_pres(medi_rec_number,pat_name,prescription,prescrip_duration) values(" + input2;
                query3 = query3 + ",'" + label14.Text.Substring(3) + "','" + form3.getInsp() + "','" + form3.getDetail() + "')";
                MySqlCommand myCommand = new MySqlCommand(query3, myConnection);
                int n = myCommand.ExecuteNonQuery();
                if (n > 0)
                {
                    try
                    {
                        ds3 = new DataSet();
                        BindingSource bs = new BindingSource();
                        string query2 = "select insp_date as '添加日期',prescription as '处方',prescrip_duration as '处方详情' from medical_record_pres where prescription != '无' && medi_rec_number = '" + input2 + "'";
                        myadp3 = new MySqlDataAdapter(query2, myConnection); //适配器   
                        myadp3.Fill(ds3, "table"); //将查询到数据填充到数据集
                        bs.DataSource = ds3.Tables["table"];
                        dataGridView3.DataSource = bs;
                    }
                    finally
                    {
                        myConnection.Close();
                    }
                }
                else
                {
                    MessageBox.Show("失败！请检查输入");
                }
                myConnection.Close();
            }
        }

        /// <summary>
        /// 科室信息管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem3_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            panel2.Visible = false;
            tabControl1.Visible = false;
            tabControl3.Visible = false;
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            if (item.Name == "科室信息查询")
            {
                tabControl3.SelectedTab = tabControl3.TabPages[0];
            }
            else
            {
                tabControl3.SelectedTab = tabControl3.TabPages[1];
            }
            tabControl3.Visible = true;
        }

        DataSet ds4 = null;
        MySqlDataAdapter myadp4 = null;
        private void textBox10_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(System.Windows.Forms.Keys.Enter))
            {
                string query = "select dep_name as '科室名',dep_address as '地址', dep_telephone as '联系电话'," +
                    "doc_name as '主管医生' from department where dep_name like '%";
                query += textBox10.Text;
                query += "%' || dep_address like '%";
                query += textBox10.Text;
                query += "%' || dep_telephone like '%";
                query += textBox10.Text;
                query += "%' || doc_name like '%";
                query += textBox10.Text + "%'";
                myConnection.Open();
                ds4 = new DataSet();
                BindingSource bs = new BindingSource();
                myadp4 = new MySqlDataAdapter(query, myConnection); //适配器   
                myadp4.Fill(ds4, "table"); //将查询到数据填充到数据集
                bs.DataSource = ds4.Tables["table"];
                dataGridView4.DataSource = bs;
                myConnection.Close();
            }
        }


        private void button9_Click(object sender, EventArgs e)
        {

        }


        DataSet ds5 = null;
        MySqlDataAdapter myadp5 = null;
        private void textBox11_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(System.Windows.Forms.Keys.Enter))
            {
                string query = "select work_number as '工作证号',doc_name as '医生姓名', dep_name as '所属科室'," +
                    "age as '年龄', title as '职称' from doctor where work_number like '%";
                query += textBox11.Text;
                query += "%' || doc_name like '%";
                query += textBox11.Text;
                query += "%' || dep_name like '%";
                query += textBox11.Text;
                query += "%' || age like '%";
                query += textBox11.Text;
                query += "%' || title like '%";
                query += textBox11.Text + "%'";
                myConnection.Open();
                ds5 = new DataSet();
                BindingSource bs = new BindingSource();
                myadp5 = new MySqlDataAdapter(query, myConnection); //适配器   
                myadp5.Fill(ds5, "table"); //将查询到数据填充到数据集
                bs.DataSource = ds5.Tables["table"];
                dataGridView5.DataSource = bs;
                myConnection.Close();
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            MySqlCommandBuilder scb = new MySqlCommandBuilder(myadp5);

            //更新数据
            try
            {
                //这里是关键
                myadp5.Update(ds5);
            }
            catch (MySqlException ex)
            {
                MessageBox.Show("修改失败，请确认修改内容是否正确");
            }
        }
        /// <summary>
        /// 用户管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 切换用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form5 form5 = new Form5();
            form5.ShowDialog();
            if (!form5.GetFlag())
            {
                //Close();
            }
            if (form5.getFlag())
            {
                科室信息管理ToolStripMenuItem.Visible = true;
            }
            else
            {
                科室信息管理ToolStripMenuItem.Visible = false;
            }
        }

        /// <summary>
        /// 查看床位，分配床位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 查看病房床位ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel2.Visible = true;
            panel1.Visible = false;
            tabControl1.Visible = false;
            tabControl2.Visible = false;
            tabControl3.Visible = false;
            myConnection.Open();
            string query = "SELECT distinct dep_name FROM department";
            MySqlCommand myCommand = new MySqlCommand(query, myConnection);
            MySqlDataReader myReader = myCommand.ExecuteReader();
            try
            {
                while (myReader.Read())
                {
                    comboBox1.Items.Add(myReader.GetString(0));
                }
            }
            finally
            {
                myReader.Close();
                myConnection.Close();
            }
        }
        DataSet ds6 = null;
        MySqlDataAdapter myadp6 = null;
        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string query = "SELECT ward_number as '病房号',bed_number as '病床号', have as '是否为空床/1为是，0为否' FROM ward where dep_name = '" + comboBox1.SelectedText +"'";
            myConnection.Open();
            ds6 = new DataSet();
            BindingSource bs = new BindingSource();
            myadp6 = new MySqlDataAdapter(query, myConnection); //适配器   
            myadp6.Fill(ds6, "table"); //将查询到数据填充到数据集
            bs.DataSource = ds6.Tables["table"];
            dataGridView6.DataSource = bs;
            myConnection.Close();
        }

        private void button11_Click(object sender, EventArgs e)
        {

            MySqlCommandBuilder scb = new MySqlCommandBuilder(myadp6);

            //更新数据
            try
            {
                //这里是关键
                myadp6.Update(ds6);
            }
            catch (MySqlException ex)
            {
                MessageBox.Show("修改失败，请确认修改内容是否正确");
            }
        }

        /// <summary>
        /// 出院结账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 出院结账ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form6 form6 = new Form6();
            form6.ShowDialog();
        }
    }
}
